package com.example.teamtribe.main.club.activity;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.teamtribe.R;
import com.example.teamtribe.database.Posting;

import java.util.List;

public class ActivityAdapter extends RecyclerView.Adapter<ActivityAdapter.ViewHolder> {

    private Context context;
    private List<Posting> activities;

    public ActivityAdapter(List<Posting> activities) {
        this.activities = activities;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_activity, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Posting action = activities.get(position);
        holder.tvTitle.setText(action.getTitle());
        if (holder.rvContend.getAdapter() == null) {
            holder.rvContend.setAdapter(new PhotosAdapter(action.getImages()));
        } else {
            ((PhotosAdapter) holder.rvContend.getAdapter()).setData(action.getImages());
        }
        holder.rvContend.getAdapter().notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return activities.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvTitle;
        private RecyclerView rvContend;

        public ViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            rvContend = itemView.findViewById(R.id.rv_content);
            rvContend.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
        }
    }
}
